package grpc

import (
	"context"
	"github.com/ecodeclub/ekit/slice"
	"google.golang.org/grpc"
	"jk-time/webook/account/domain"
	"jk-time/webook/account/service"
	accountv1 "jk-time/webook/api/proto/gen/account/v1"
)

type AccountServiceServer struct {
	accountv1.UnimplementedAccountSerivceServer
	svc service.AccountService
}

func NewAccountServiceServer(svc service.AccountService) *AccountServiceServer {
	return &AccountServiceServer{svc: svc}
}

func (w *AccountServiceServer) Register(server *grpc.Server) {
	accountv1.RegisterAccountSerivceServer(server, w)
}
func (w *AccountServiceServer) Credit(ctx context.Context, in *accountv1.CreditRequest) (*accountv1.CreditResponse, error) {
	err := w.svc.AddCredit(ctx, domain.Creadit{
		Biz:   in.Biz,
		BizId: in.BizId,
		Items: slice.Map(in.Items, func(idx int, src *accountv1.CredItem) domain.CreditItem {
			return domain.CreditItem{
				Account: src.Account,
				Amt:     src.Amt,
				Uid:     src.Uid,
				// 两者取值都是一样的，偷懒，直接转
				AccountType: domain.AccountType(src.AccoutType),
				Currency:    src.Currency,
			}
		}),
	})
	return &accountv1.CreditResponse{}, err
}
